package com.banana.springcloud.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 以编码的方式实现路由
 * 官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#gatewayfilter-factories
 * @author layman
 * @date 2021/1/19
 */
@Configuration
public class GateWayConfig {

    /**
     * 配置一个ID为 first 的路由规则
     * 当访问地址:http://localhost:9527/guonei 时会自动转发到地址 http://news.baidu.com/guonei
     * @param routeLocatorBuilder
     * @return RouteLocator
     */
    @Bean
    public RouteLocator CustomerRouteLocatorFirst(RouteLocatorBuilder routeLocatorBuilder){
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("first",r->r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
        return routes.build();
    }
    /**
     * 配置一个ID为 second 的路由规则
     * 当访问地址:http://localhost:9527/guoji 时会自动转发到地址 http://news.baidu.com/guoji
     */
    @Bean
    public RouteLocator CustomerRouteLocatorSecond(RouteLocatorBuilder routeLocatorBuilder){
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("second",r->r.path("/guoji").uri("http://news.baidu.com/guoji")).build();
        return routes.build();
    }
}
